/*

*/

clear all

adopath ++/afs/umich.edu/group/m/mmcmps/ado

// MACROS
global funding "Y:/limited/Michigan_CTE/funding_change"


***
*  B1 CEPD MONEY 
***

use "Y:/limited/Michigan_CTE/funding_change/data_final/dcode_funding.dta", clear

foreach v in cte_funds_state60 cte_funds_cepd40 cte_funds_total {
	gen all_`v' = dcode_`v' + isd_`v'
}

foreach v of varlist pct_econdis pct_nonwhite pct_cte_any pct_cte_comp av_cte_courses   {
  	bys cepd year: gegen temp = mean(`v') [fw=hs_students]
	drop `v'
	ren temp `v'
}

bys cepd year: gegen urbanicity = min(d_urban)

gcollapse (sum) all_cte_funds_state60 all_cte_funds_cepd40 all_cte_funds_total hs_students ///
		(mean) cepd_pct_poor = district_pct_poor cepd_pct_minority = district_pct_min  (min) cepd_urbanicity = district_urbanicity, by(cepd year)


keep if inrange(year,2012, 2019)

foreach v of varlist all_cte_funds_* {
	bys year: gegen all_`v' = total(`v')
	gen temp = `v'/all_`v'*100

	bys cepd: gegen pre_`v'  = mean(cond(year<2015,`v',.))
	bys cepd: gegen post_`v' = mean(cond(year>=2017,`v',.))

	bys cepd: gegen pre_pct_`v'  = mean(cond(year<2015,temp,.))
	bys cepd: gegen post_pct_`v' = mean(cond(year>=2017,temp,.))

	gen ps_`v'=`v'/hs_students
	
	bys cepd: gegen pre_ps_`v'  = mean(cond(year<2015,`v'/hs_students,.))
	bys cepd: gegen post_ps_`v' = mean(cond(year>=2017,`v'/hs_students,.))

	drop temp
}

foreach v of varlist pre* post* {
	replace `v'=0 if `v'==.
}

local q = 5
gquantiles q5 = pct_econdis if year==2014, nq(`q') xtile
bys cepd: gegen mostpoor = max(q5==`q')


preserve

forval i = 2012/2019 {
	gen c`i' = year==`i' 
}
replace c2014 = 0 

// RUN REGRESSIONS
gen cohort = .
gen fund0_over_time = .
gen fund0_over_time_l95 = .
gen fund0_over_time_u95 = .
gen fund1_over_time = .
gen fund1_over_time_l95 = .
gen fund1_over_time_u95 = .
gen funds0_over_time = .
gen funds0_over_time_l95 = .
gen funds0_over_time_u95 = .
gen funds1_over_time = .
gen funds1_over_time_l95 = .
gen funds1_over_time_u95 = .
gen fundc0_over_time = .
gen fundc0_over_time_l95 = .
gen fundc0_over_time_u95 = .
gen fundc1_over_time = .
gen fundc1_over_time_l95 = .
gen fundc1_over_time_u95 = .


eststo clear
foreach rank in 0 1 {
	eststo: reghdfe ps_all_cte_funds_total c2012-c2019 if mostpoor==`rank' [fw=hs_students], a(cepd) cluster(cepd)
	local i = 1
	forvalues x = 2012/2019 {
	  replace fund`rank'_over_time =  _b[c`x'] in `i'
	  replace fund`rank'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace fund`rank'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace cohort = `x' if cohort==. in `i'
	  local ++i
	}
	eststo: reghdfe ps_all_cte_funds_state c2012-c2019 if mostpoor==`rank' [fw=hs_students], a(cepd) cluster(cepd)
	local i = 1
	forvalues x = 2012/2019 {
	  replace funds`rank'_over_time =  _b[c`x'] in `i'
	  replace funds`rank'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace funds`rank'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  local ++i
	}

	eststo: reghdfe ps_all_cte_funds_cepd c2012-c2019 if mostpoor==`rank' [fw=hs_students], a(cepd) cluster(cepd)
	local i = 1
	forvalues x = 2012/2019 {
	  replace fundc`rank'_over_time =  _b[c`x'] in `i'
	  replace fundc`rank'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace fundc`rank'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  local ++i
	}
}

keep cohort fund0*  fund1*  funds0*  funds1*  fundc0* fundc1* 
drop if cohort==.



// DRAW GRAPH 
tw (rarea fund0_over_time_l95 fund0_over_time_u95 cohort, color("$color1int") lwidth(none)) ///
   (rarea fund1_over_time_l95 fund1_over_time_u95 cohort, color("$color4int") lwidth(none)) ///
   (connected fund0_over_time cohort, sort lc("$color1") lp(solid) mc("$color1")) ///
   (connected fund1_over_time cohort, sort lc("$color4") lp(shortdash) mc("$color4")) ///
   (pci -30 2015.2 45 2015.2 (1) "Funding Change", lc(gs8) recast(pccapsym) msymbol(i) mlabc(gs8) mlabs(medlarge)) ///
   (pci -30 2014.7 45 2014.7, lc(gs8%14) lw(22)), ///
   xlabel(2012(1)2019, labsize(medlarge)) ///
   xtitle(" " "Academic  Year", size(medlarge)) ///
   ylabel(-30(15)45, angle(0) gmax glc(gs15) glw(thin) labsize(medlarge)) ///
   ytitle("Change in Per-Student CTE Dollars" "(All State Sources)" " " , size(medlarge)) ///
   legend(order(3 "Wealthiest 80% of  CEDPs" 4 "Poorest 20% of CEDPs") rows(1)  size(medlarge)) ///
   graphregion(color(white)) bgcolor(white) plotregion(margin( 5 5 0 0)) xsize(8)	  

graph save ${funding}/output/graphs/B6_all_funding.gph, replace
cap mkdir ${funding}/output/state
graph export ${funding}/output/state/cepd_all_funding.pdf, replace 
   
// DRAW GRAPH 
tw (rarea funds0_over_time_l95 funds0_over_time_u95 cohort, color("$color1int") lwidth(none)) ///
   (rarea funds1_over_time_l95 funds1_over_time_u95 cohort, color("$color4int") lwidth(none)) ///
   (connected funds0_over_time cohort, sort lc("$color1") lp(solid) mc("$color1")) ///
   (connected funds1_over_time cohort, sort lc("$color4") lp(shortdash) mc("$color4")) ///
   (pci -30 2015.2 45 2015.2 (1) "Funding Change", lc(gs8) recast(pccapsym) msymbol(i) mlabc(gs8) mlabs(medlarge)) ///
   (pci -30 2014.7 45 2014.7, lc(gs8%14) lw(23)), ///
   xlabel(2012(1)2019, labsize(medlarge)) ///
   xtitle(" " "Academic Year", size(medlarge)) ///
   ylabel(-30(15)45, angle(0) gmax glc(gs15) glw(thin) labsize(medlarge)) ///
   ytitle("Change in Per-Student CTE Dollars" "(State-Formula Funding)" " " , size(medlarge)) ///
   legend(order(3 "Wealthiest 80% of  CEDPs" 4 "Poorest 20% of CEDPs") rows(1)  size(medlarge)) ///
   graphregion(color(white)) bgcolor(white) plotregion(margin( 5 5 0 0)) xsize(8)	  

 
graph save ${funding}/output/graphs/B6a_formula_funding.gph, replace
graph export ${funding}/output/state/cepd_formula_funding.pdf, replace
  
   
// DRAW GRAPH 
tw (rarea fundc0_over_time_l95 fundc0_over_time_u95 cohort, color("$color1int") lwidth(none)) ///
   (rarea fundc1_over_time_l95 fundc1_over_time_u95 cohort, color("$color4int") lwidth(none)) ///
   (connected fundc0_over_time cohort, sort lc("$color1") lp(solid) mc("$color1")) ///
   (connected fundc1_over_time cohort, sort lc("$color4") lp(shortdash) mc("$color4")) ///
   (pci -30 2015.2 45 2015.2 (1) "Funding Change", lc(gs8) recast(pccapsym) msymbol(i) mlabc(gs8) mlabs(medlarge)) ///
   (pci -30 2014.7 45 2014.7, lc(gs8%14) lw(23)), ///
   xlabel(2012(1)2019, labsize(medlarge)) ///
   xtitle(" " "Academic Year", size(medlarge)) ///
   ylabel(-30(15)45, angle(0) gmax glc(gs15) glw(thin) labsize(medlarge)) ///
   ytitle("Change in Per-Student CTE Dollars" "(Discretionary CEPD Funding)" " " , size(medlarge)) ///
   legend(order(3 "Wealthiest 80% of  CEDPs" 4 "Poorest 20% of CEDPs") rows(1)  size(medlarge)) ///
    graphregion(color(white)) bgcolor(white) plotregion(margin( 5 5 0 0)) xsize(8)	  
	  

	  
graph save ${funding}/output/graphs/B6b_cepd_funding.gph, replace
graph export ${funding}/output/state/cepd_cepd_funding.pdf, replace

restore




  
  